﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilStartUps.DAL;

namespace FilStartUps.Forum
{
    public partial class ForumDisplay : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetForumDetails();
            }
        }

        public void GetForumDetails()
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                var forum = (from f in db_connect.ForumGroups
                             let cCount = (from fr in db_connect.Forums
                                           where fr.forumgroupidFK.Equals(f.forumgroupidPK)
                                           select fr).Count()
                             where cCount >= 1
                             select f).ToList();

                rpforumgroup.DataSource = forum;
                rpforumgroup.DataBind();
            }
        }

        public List<Forum_Display> GetForum(string forumgroupidFK)
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                int forumid = int.Parse(forumgroupidFK);
                var forum = (from f in db_connect.Forums
                             where f.forumgroupidFK.Equals(forumid)
                             let cCount =
                             (from ft in db_connect.ForumThreads
                              where ft.forumidFK.Equals(f.forumidPK)
                              select ft).Count()
                             select new Forum_Display
                             {
                                 views = f.views,
                                 datecreated = f.datecreated,
                                 forumdescription = f.forumdescription,
                                 forumidPK = f.forumidPK,
                                 forumtitle = f.forumtitle,
                                 totalthread = cCount
                             }).ToList();

                return forum;
            }
            //var forum = (from ft in db_connect.ForumThreads
            //              where ft.forumidFK == forumidPK 
            //            let cCount =
            //               (from fr in db_connect.ForumReplies
            //                where ft.forumthreadidPK == fr.forumthreadidFK
            //                select fr).Count()
            //            select new 
            //            {
            //                forumthreadidPK =ft.forumthreadidPK,
            //                forumdescription = ft.forumdescription,
            //                totalpost = cCount,
            //                forumtitle = ft.forumtitle,
            //                forumidFK = ft.forumidFK,
            //                totalviews = ft.views
            //            }).ToList();

        }

        protected void rpForumList_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "SelectRow")
            {
                Repeater rpForumList = (Repeater)rpforumgroup.Items[e.Item.ItemIndex].FindControl("rpForumlist");

                UpdateViewsForum(Convert.ToInt32(e.CommandArgument));
                Response.Redirect("~/Forum/Thread.aspx?t=" + Convert.ToInt32(e.CommandArgument));
            }
        }
        private void UpdateViewsForum(int forumidFK)
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                var forum = db_connect.Forums.Where(f => f.forumidPK.Equals(forumidFK)).SingleOrDefault();
                forum.views += 1;
                db_connect.SaveChanges();
            }
        }
    }
}